$(function () {
    // 加载左侧菜单
    initLeftTree();
    getStore();
})

function getUserInfo() {
    $.axse(hostUrl + "/user/getUserInfo",
        null,
        function (res) {
            if (res.status == 0) {
                var data = res.data;
                // 角色
                var roles = data.sysRoles;
                if (roles != null && roles.length > 0) {
                    $("#roleSpanIndex").text(roles[0].roleName);
                }
                // 用户信息
                var userInfos = data.sysUserInfo;
                // 头像信息
                if (!$.common.isEmpty(data.headImgUrl)) {
                    $("#headImgUrl").attr("src", data.headImgUrl);
                }
                // 用户名
                $("#userName").text(userInfos.userName);
                // 用户类型 不同 展示不同首页
                if (userInfos.accountType == 0) {
                    // 经销商
                    $("#defaultFrame").attr("src", hostUrl + "/index/console_jxs.do");
                } else {
                    $("#defaultFrame").attr("src", hostUrl + "/index/console.do");
                }
            } else {
                Layer.showFailAlert(res.msg, 0);
            }
        },
        function () {
            Layer.showFailAlert("请求发送失败，请稍后重试");
        })
}

function getStore(formIndex) {
    $.axsefalse(hostUrl + "/indexPage/getStore",
        null,
        function (res) {
            if (res.status == 0) {
                var data = res.data;
                var html = "";
                if (data) {
                    if (!window.localStorage) {
                        Layer.showFailAlert("当前浏览器不支持某些特性，为了更好的体验，请使用谷歌浏览器或火狐浏览器");;
                        return false;
                    } else {
                        var storage = window.localStorage;
                        if (data.length == 1) {
                            // 只有一家店
                            html += "<option value='" + data[0].id + "'>" + data[0].storeName + "</option>";
                            storage.setItem("dmsStoreId", data[0].id);
                            storage.setItem("dmsDealerId", data[0].dealerId);
                            //globalStoreId = data[0].id;
                            //globalDealerId = data[0].dealerId;
                        } else if (data.length > 1) {
                            // 多家店
                            storage.setItem("dmsStoreId", res.msg);
                            storage.setItem("dmsDealerId", data[0].dealerId);
                            //globalStoreId = res.msg;// 默认选择自己的店
                            //globalDealerId = data[0].dealerId;
                            html += "<option value='all'>全部门店</option>";
                            for (var i = 0; i < data.length; i++) {
                                if (data[i].id == res.msg) {
                                    html += "<option selected value='" + data[i].id + "'>" + data[i].storeName + "</option>";
                                } else {
                                    html += "<option value='" + data[i].id + "'>" + data[i].storeName + "</option>";
                                }
                            }
                        }
                    }
                }

                if (html.length != 0) {
                    $("#storeSelect").html(html);
                } else {
                    // 没找到门店 或者 没门店
                    $("#storeSelect").empty();
                    $("#storeSelect").parent().attr("class", "layui-select layui-hide");
                }
            } else {
                Layer.showFailAlert(res.msg, 0);
            }
        },
        function () {
            Layer.showFailAlert("请求发送失败，请稍后重试");
        })
}

function initLeftTree() {
    $.axse(hostUrl + "/sys/getMenusByUserId",
        null,
        function (res) {
            if (res.status == 0) {
                var html = "";
                var data = res.data;
                for (var i = 0; i < data.length; i++) {
                    html += "<li data-name='car_source_management' class='layui-nav-item'>";
                    if (data[i].url != null && data[i].url.length > 0) {
                        html += "<a lay-href='" + data[i].url + "' lay-tips='" + data[i].name + "' lay-direction='2'>";
                        html += "<i class='" + data[i].className + "'></i>"
                        html += "<cite>" + data[i].name + "</cite>";
                        html += "</a>";
                    } else {
                        html += "<a href='javascript:;' lay-tips='" + data[i].name + "'>";
                        html += "<i class='" + data[i].className + "'></i>"
                        html += "<cite>" + data[i].name + "</cite>"
                        html += "</a>"
                    }

                    for (var j = 0; j < data[i].childrenList.length; j++) {
                        html += "<dl class='layui-nav-child'>";
                        html += "<dd><a lay-href='" + data[i].childrenList[j].url + "'>" + data[i].childrenList[j].name + "</a></dd>"
                        html += "</dl>";
                    }
                    html += "</li>";
                }
                $("#LAY-system-side-menu").html(html);
            } else {
                Layer.showFailAlert(res.msg, 0);
            }
        },
        function () {
            Layer.showFailAlert("网络开小差了，请稍后重试");
        }
    )
}
